package com.amap.url.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.util.Log;

import com.amap.model.Location;

public class HttpRequestUtil {

	
	public static String getFileContent(String url) {
		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet(url);

		try {
			HttpResponse response = httpclient.execute(httpget);
			HttpEntity entity = response.getEntity();

			if (entity != null) {
				InputStream instream = entity.getContent();
				String result = toString(instream);

				instream.close();
				Log.d("JSON", result);
				return result;
			}
		} catch (Exception e) {
			Log.e("NGVL", "Falha ao acessar service", e);
		}
		return null;
	}
	

	private static String toString(InputStream is) 
			throws IOException{

		byte[] bytes = new byte[1024];
		ByteArrayOutputStream baos = 
				new ByteArrayOutputStream();
		int lidos;
		while ((lidos = is.read(bytes)) > 0){
			baos.write(bytes, 0, lidos);
		}
		return new String(baos.toByteArray());
	}
	
	
	public static String mountUrl(Location location, String radius, String type, String name ){
		
		String locationAux = String.valueOf(location.getLatitude()) + "," + String.valueOf(location.getLongitude());
		
		String url = "https://maps.googleapis.com/maps/api/place/search/json?location=" + locationAux +
				"&radius="+ radius +"&types="+ type +"&name="+ name +"&sensor=false&key=AIzaSyDMMrQW2VBYz6gX6b2mMJSzfrcLJmQE0mY";
		
		
		Log.d("URL", url);
		
		return url;
		
	}
	
	
}